/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.helper;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.scripting.xmltags.IfSqlNode;
import org.apache.ibatis.scripting.xmltags.MixedSqlNode;
import org.apache.ibatis.scripting.xmltags.SetSqlNode;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.scripting.xmltags.TrimSqlNode;
import org.apache.ibatis.scripting.xmltags.WhereSqlNode;
import org.apache.ibatis.session.Configuration;

import com.mapperdb.db.ResultEntity;

public class SqlNodeHelper {
	protected Configuration configuration;

	public SqlNodeHelper(Configuration configuration) {
		this.configuration = configuration;
	}

	public static SqlNodeHelper use(Configuration configuration) {
		return new SqlNodeHelper(configuration);
	}

	/**
	 * WhereSqlNode
	 * @param contents
	 * @return
	 */
	public SqlNode newWhereSqlNode(List<SqlNode> contents) {
		return newWhereSqlNode(new MixedSqlNode(contents));
	}

	/**
	 * WhereSqlNode
	 * @param contents
	 * @return
	 */
	public SqlNode newWhereSqlNode(SqlNode contents) {
		return new WhereSqlNode(configuration, contents);
	}

	/**
	 * @Title: WhereSqlNode
	 * @param sql
	 * @return: SqlNode
	 */
	public SqlNode newWhereSqlNode(String sql) {
		return newWhereSqlNode(new StaticTextSqlNode(sql));
	}

	/**
	 * TrimSqlNode
	 * @param contents
	 * @param prefix
	 * @param prefixesToOverride
	 * @param suffix
	 * @param suffixesToOverride
	 * @return
	 */
	public SqlNode newTrimSqlNode(List<SqlNode> contents, String prefix, String prefixesToOverride, String suffix,
			String suffixesToOverride) {
		return newTrimSqlNode(new MixedSqlNode(contents), prefix, prefixesToOverride, suffix, suffixesToOverride);
	}

	/**
	 * TrimSqlNode
	 * @param contents
	 * @param prefix
	 * @param prefixesToOverride
	 * @param suffix
	 * @param suffixesToOverride
	 * @return
	 */
	public SqlNode newTrimSqlNode(SqlNode contents, String prefix, String prefixesToOverride, String suffix,
			String suffixesToOverride) {
		return new TrimSqlNode(configuration, contents, prefix, prefixesToOverride, suffix, suffixesToOverride);
	}

	/**
	 * SetSqlNode
	 * @param contents
	 * @return
	 */
	public SqlNode newSetSqlNode(List<SqlNode> contents) {
		return newSetSqlNode(new MixedSqlNode(contents));
	}

	/**
	 * SetSqlNode
	 * @param contents
	 * @return
	 */
	public SqlNode newSetSqlNode(SqlNode contents) {
		return new SetSqlNode(configuration, contents);
	}

	/**
	 * 获取所有列的where节点中的if判断列
	 *
	 * @param entityClass
	 * @return
	 */
	public static SqlNode getIfColumnNodeAll(ResultEntity resultEntity) {
		List<SqlNode> ifNodes = new ArrayList<SqlNode>();
		//获取全部列
		Result[] results = resultEntity.results();
		//对所有列循环，生成<if test="property!=null">column = #{property}</if>
		for (Result column : results) {
			ifNodes.add(getIfNotNull(column, getColumnEqualsProperty(column)));
		}
		return new MixedSqlNode(ifNodes);
	}

	/**
	 * 获取所有列的where节点中的if判断列
	 *
	 * @param entityClass
	 * @return
	 */
	public static SqlNode getPkColumnNode(ResultEntity resultEntity) {
		List<SqlNode> ifNodes = new ArrayList<SqlNode>();
		//获取全部列
		Result[] results = resultEntity.results();
		//对所有列循环，生成<if test="property!=null">column = #{property}</if>
		for (Result column : results) {
			if (column.id()) ifNodes.add(getIfNotNull(column, getColumnEqualsProperty(column)));
		}
		return new MixedSqlNode(ifNodes);
	}

	/**
	 * 返回if条件的sqlNode
	 * <p>一般类型：<code>&lt;if test="property!=null"&gt;columnNode&lt;/if&gt;</code></p>
	 *
	 * @param column
	 * @param columnNode
	 * @param empty      是否包含!=''条件
	 * @return
	 */
	public static SqlNode getIfNotNull(Result result, SqlNode columnNode) {
		return getIfNotNull(result.property(), columnNode);
	}

	public static SqlNode getIfNotNull(Result result, String text) {
		return getIfNotNull(result.property(), new StaticTextSqlNode(text));
	}

	public static SqlNode getIfNotNull(String property, String text) {
		return getIfNotNull(property, new StaticTextSqlNode(text));
	}

	public static SqlNode getIfNotNull(String property, SqlNode columnNode) {
		return new IfSqlNode(columnNode, property + " != null");
	}

	/**
	 * 获取 <code>AND column = #{property}</code>
	 *
	 * @param column
	 * @param first
	 * @return
	 */
	public static SqlNode getColumnEqualsProperty(Result result) {
		return new StaticTextSqlNode(" AND " + ResultHelper.formatPropertyEqColumn(result));
	}
}